#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include <assert.h>

template<typename T>
class Singleton
{
   static T* ms_singleton;
   public:
      Singleton()
      {
         assert(!ms_singleton);
         //use a cunning trick to get the singleton pointing to the start of
         //the whole, rather than the start of the Singleton part of the object
         int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
         ms_singleton = (T*)((int)this + offset);
      }
      ~Singleton()
      {
         assert(ms_singleton);
         if(ms_singleton)
            delete ms_singleton;
         ms_singleton=0;
      }
      static T& getRef()
      {
          if(!ms_singleton)
              ms_singleton = new T;
          return *ms_singleton;
      }
      static T* getInst()
      {
          if(!ms_singleton)
              ms_singleton = new T;
         return ms_singleton;
      }
};

template <typename T> T* Singleton <T>::ms_singleton = 0;

#endif
